﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vhcs.Data.Dto;
using Vhcs.Data.Dao;

namespace Vhcs.Data
{
    public class MailAction
    {
        private static MailTemplateDao dao = new MailTemplateDao();

        public static MailTemplateDTO GetMailTemplate(string mailcode)
        {
            return dao.ReadMailTemplate(mailcode);
        }

        public static MailTemplateDTO GetOriginalMailTemplateAndSignature(string mailcode, string signatureCode)
        {
            return dao.ReadMailTemplateWithSignature(mailcode, signatureCode);
        }

        public static MailDTO GetMailNotify(string TicketName, string code, string status, string approver, string creator, string ticketContent, string url){

            MailTemplateDTO mailNotify = dao.ReadMailTemplate("K2-Notify");
            // Modify Mail Subject 
            string mailSubject = mailNotify.TitleVN;
            mailSubject = mailSubject.Replace("$TicketName$", TicketName);
            mailSubject = mailSubject.Replace("$Code$", code);
            mailSubject = mailSubject.Replace("$Status$", status);
            mailSubject = mailSubject.Replace("$Approver$", approver);
            mailSubject = mailSubject.Replace("$Creator$", creator);
            // Modify Mail Content
            string mailContent = mailNotify.ContentVN;
            mailContent = mailContent.Replace("$TicketName$", TicketName);
            mailContent = mailContent.Replace("$ContentTicket$", ticketContent);
            mailContent = mailContent.Replace("$Status$", status);
            mailContent = mailContent.Replace("$Approver$", approver);
            mailContent = mailContent.Replace("$url$", url);

            // Get Signature
            string mailSignature = dao.ReadMailTemplate("K2-Signature").ContentVN;

            // Create mail data
            MailDTO result = new MailDTO();
            result.MailCode = mailNotify.MailCode;
            result.Subject = mailSubject;
            result.Content = mailContent;
            result.Signature = mailSignature;

            return result;
        }

        public static MailDTO GetMailWaiting(string TicketName, string processCode, string creator, string ticketContent, string url)
        {

            MailTemplateDTO mailWaiting = dao.ReadMailTemplate("K2-Waiting");
            // Modify Mail Subject 
            string mailSubject = mailWaiting.TitleVN;
            mailSubject = mailSubject.Replace("$TicketName$", TicketName);
            mailSubject = mailSubject.Replace("$Code$", processCode);
            mailSubject = mailSubject.Replace("$Creator$", creator);
            // Modify Mail Content
            string mailContent = mailWaiting.ContentVN;
            mailContent = mailContent.Replace("$TicketName$", TicketName);
            mailContent = mailContent.Replace("$Creator$", creator);
            mailContent = mailContent.Replace("$ContentTicket$", ticketContent);
            mailContent = mailContent.Replace("$url$", url);

            // Get Signature
            string mailSignature = dao.ReadMailTemplate("K2-Signature").ContentVN;

            // Create mail data
            MailDTO result = new MailDTO();
            result.MailCode = mailWaiting.MailCode;
            result.Subject = mailSubject;
            result.Content = mailContent;
            result.Signature = mailSignature;

            return result;
        }

        public static MailDTO GetMailSubmit(string TicketName, string processCode, string ticketContent, string approver, string url)
        {

            MailTemplateDTO mailWaiting = dao.ReadMailTemplate("K2-Submit");
            // Modify Mail Subject 
            string mailSubject = mailWaiting.TitleVN;
            mailSubject = mailSubject.Replace("$TicketName$", TicketName);
            mailSubject = mailSubject.Replace("$Code$", processCode);
            // Modify Mail Content
            string mailContent = mailWaiting.ContentVN;
            mailContent = mailContent.Replace("$TicketName$", TicketName);
            mailContent = mailContent.Replace("$Approver$", approver);
            mailContent = mailContent.Replace("$ContentTicket$", ticketContent);
            mailContent = mailContent.Replace("$url$", url);

            // Get Signature
            string mailSignature = dao.ReadMailTemplate("K2-Signature").ContentVN;

            // Create mail data
            MailDTO result = new MailDTO();
            result.MailCode = mailWaiting.MailCode;
            result.Subject = mailSubject;
            result.Content = mailContent;
            result.Signature = mailSignature;

            return result;
        }

        public static MailDTO GetMailAlertExpire(string RequestName,string Creator,string RequestID,string Folio,string ApproveUrl)
        {

            MailTemplateDTO mailexpire = dao.ReadMailTemplate("K2_AlertExpire");
            // Modify Mail Subject 
            string mailSubject = mailexpire.TitleVN;

            mailSubject = mailSubject.Replace("$RequestName$", RequestName);
            mailSubject = mailSubject.Replace("$RequestID$", RequestID);
            mailSubject = mailSubject.Replace("$CreatorName$", Creator);

            // Modify Mail Content
            string mailContent = mailexpire.ContentVN;
            mailContent = mailContent.Replace("$RequestName$", RequestName);
            mailContent = mailContent.Replace("$Folio$", Folio);
            mailContent = mailContent.Replace("$url$", ApproveUrl);

            // Get Signature
            string mailSignature = dao.ReadMailTemplate("K2-Signature").ContentVN;

            // Create mail data
            MailDTO result = new MailDTO();
            result.MailCode = mailexpire.MailCode;
            result.Subject = mailSubject;
            result.Content = mailContent;
            result.Signature = mailSignature;

            return result;
        }

        public static MailDTO GetMailExpire(string RequestName,string RequestID,string Folio,string HistoryUrl)
        {

            MailTemplateDTO mailexpire = dao.ReadMailTemplate("K2_Expire");
            // Modify Mail Subject 
            string mailSubject = mailexpire.TitleVN;
            mailSubject = mailSubject.Replace("$RequestName$", RequestName);
            mailSubject = mailSubject.Replace("$RequestID$", RequestID);
            //mailSubject = mailSubject.Replace("$CreatorName$", Creator);
            // Modify Mail Content
            string mailContent = mailexpire.ContentVN;
            mailContent = mailContent.Replace("$RequestName$", RequestName);
            mailContent = mailContent.Replace("$Folio$", Folio);
            mailContent = mailContent.Replace("$url$", HistoryUrl);

            // Get Signature
            string mailSignature = dao.ReadMailTemplate("K2-Signature").ContentVN;

            // Create mail data
            MailDTO result = new MailDTO();
            result.MailCode = mailexpire.MailCode;
            result.Subject = mailSubject;
            result.Content = mailContent;
            result.Signature = mailSignature;

            return result;
        }
    }
}
